<?php
/**
*
* @package footprintdiary
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
* Minimum Requirement: PHP 4.3.3
*/
include('constants.class.php');

class database {

  public $conn;

  function database(){
    $this->conn = mysql_connect(constants::$db_ip, constants::$db_user, constants::$db_pass) or
    die("IP: ".constants::$db_ip.constants::$db_user.constants::$db_pass.".");
    mysql_select_db(constants::$db_name, $this->conn);
    $database->conn = $this->conn;
  }

  function getConn() {
    $this->conn = mysql_connect(constants::$db_ip, constants::$db_user, constants::$db_pass);
    mysql_select_db(constants::$db_name, $this->conn);
    return $this->conn;
  }

  function addVisit($user){
    mysql_query('UPDATE visits SET `visits`=visits+1 WHERE `user`='.$user, $this->conn);
  }

  function checkVisits($user){
    $result = mysql_query('SELECT visits FROM visits WHERE `user`='.$user, $this->conn);
    $row = mysql_fetch_assoc($result);
    return $row['visits'];
  }

  function checkVisitTime($user){
    $result = mysql_query('SELECT timestamp FROM visits WHERE `user`='.$user, $this->conn);
    $row = mysql_fetch_assoc($result);
    return $row['timestamp'];
  }

  function checkToAddTree($user){
    if($this->checkVisits($user) >= constants::$totVisits){
      $this->resetVisits($user);
      $this->addTree($user);
      return true;
    } else {
      return false;
    }
  }

  function addTree($user){
    mysql_query('UPDATE trees SET `num`=num+1 WHERE `user`='.$user, $this->conn);
  }

  function getTrees($user){
    $result = mysql_query('SELECT num FROM trees WHERE `user`='.$user, $this->conn);
    $row = mysql_fetch_assoc($result);
    return $row['num'];
  }

  function getTotalTrees(){
    $result = mysql_query('SELECT num FROM trees', $this->conn);
    $trees = 0;
    while ($row = mysql_fetch_assoc($result)) {
      $trees += $row['num'];
    }
    return $trees;
  }

  function resetVisits($user){
    mysql_query('UPDATE visits SET `visits`=0 WHERE `user`='.$user, $this->conn);
  }

  function addUser($user){
    mysql_query('INSERT INTO visits SET `visits`=0 WHERE `user`='.$user, $this->conn);
    if(!$this->checkUserInTrees($user))
      mysql_query('INSERT INTO trees SET `num`=0 WHERE `user`='.$user, $this->conn);
  }

  function removeUser($user){
    mysql_query('DELETE FROM visits WHERE `user`='.$user, $this->conn);
  }

  function checkUserInTrees($user){
    $result = mysql_query('SELECT num FROM trees WHERE `user`='.$user, $this->conn);
    if($result) return true;
    else return false;
  }

  function totalPlantedTrees(){
    $result = mysql_query('SELECT num FROM planted_trees', $this->conn);
    $trees = 0;
    while ($row = mysql_fetch_assoc($result)) {
      $trees += $row['num'];
    }
    return $trees;
  }

  function totalUnplantedTrees(){
    return $this->getTotalTrees() - $this->totalPlantedTrees();
  }

}
$database = new database();
?>